﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ReplyContact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
                BindGrid();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
            this.div_reply.Visible = false;
        }
        else
        {
        }
    }
    public void BindGrid()
    {
        //load BusCataloge List
        ProContact pct = new ProContact();
        DataSet ds = pct.LoadContactPending();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListContact.DataSource = ds.Tables[0];
            this.grdListContact.DataBind();
            this.grdListContact.Rows[0].Visible = false;
        }
        else
        {
            this.grdListContact.DataSource = ds.Tables[0];
            this.grdListContact.DataBind();
            this.grdListContact.HorizontalAlign = HorizontalAlign.Center;
            this.grdListContact.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            this.grdListContact.HeaderRow.Cells[0].Width = 20;
            this.grdListContact.HeaderRow.Cells[1].Width = 20;
            this.grdListContact.HeaderRow.Cells[2].Width = 30;
            this.grdListContact.HeaderRow.Cells[2].Text = "ID";
            this.grdListContact.HeaderRow.Cells[3].Width = 200;
            this.grdListContact.HeaderRow.Cells[3].Text = "Name";
            this.grdListContact.HeaderRow.Cells[4].Width = 50;
            this.grdListContact.HeaderRow.Cells[4].Text = "Status";
        }
    }
    protected void grdListContact_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        this.grdListContact.SelectedIndex = Int32.Parse(e.CommandArgument.ToString());
        this.div_reply.Visible = true;
    }
    protected void grdListContact_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ProContact pcont = new ProContact();
        EnContact cont = new EnContact();
        cont.Id = Int32.Parse(this.grdListContact.SelectedRow.Cells[2].Text);
        Boolean bResult = pcont.DeleteCont(cont);
        if (bResult == true)
        {
            this.BindGrid();
            this.lblError.Text = Constant.MESSAGE_DELETE_SUCCESS;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_DELETE_ERROR;
        }
    }

    protected void grdListContact_SelectedIndexChanged(object sender, EventArgs e)
    {
        ProContact pcont = new ProContact();
        DataSet ds = new DataSet();
        ds = pcont.LoadInfoCont(Int32.Parse(this.grdListContact.SelectedRow.Cells[2].Text));
        this.lblName.Text = ds.Tables[0].Rows[0]["name"].ToString();
        this.lblEmail.Text = ds.Tables[0].Rows[0]["email"].ToString();
        this.lblPhone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
        this.lblAddress.Text = ds.Tables[0].Rows[0]["address"].ToString();
        this.lblContent.Text = ds.Tables[0].Rows[0]["comment"].ToString();
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (txtReply.Text != "")
        {

            ProSendMail psm = new ProSendMail();
            String contentreply = "Your contact: <br />" + this.lblContent.Text + "<br /><br />Answer: <br />" + this.txtReply.Text;
            psm.sendmailReplyContact(this.lblEmail.Text, "Reply your contact in TechData's Website", contentreply);
            this.lblError.Text = Constant.MESSAGE_SENDMAIL_OK;
            this.txtReply.Text = "";
            ProContact pcont = new ProContact();
            pcont.ChangeStatus(Int32.Parse(this.grdListContact.SelectedRow.Cells[2].Text));
            BindGrid();
        }
        else 
        {
            return;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.div_reply.Visible = false;
        this.grdListContact.SelectedIndex = -1;
    }
}
